package com.clgk.wechatmall.common.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import com.clgk.wechatmall.common.constants.Constants;

public class AuthFilter implements Filter {

	private static List<String> passUrl = new ArrayList<String>();

	static {
		passUrl.add("login.html");
		passUrl.add("adminLogin");
	}

//	@Override
	public void destroy() {
	}

//	@Override
	public void doFilter(ServletRequest servletRequest,ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpSession session = request.getSession();
		response.addHeader("P3P", "CP=CAO PSA OUR");
		String url = request.getServletPath();
		// 获取文件后缀
		String extension = FilenameUtils.getExtension(url);
		// 不过滤静态资源
		if ("jpg".equalsIgnoreCase(extension)
				|| "png".equalsIgnoreCase(extension)
				|| "gif".equalsIgnoreCase(extension)
				|| "js".equalsIgnoreCase(extension)
				|| "css".equalsIgnoreCase(extension)
				|| "xls".equalsIgnoreCase(extension)
				|| "swf".equalsIgnoreCase(extension)
				|| "tpl".equalsIgnoreCase(extension)
				|| "woff".equalsIgnoreCase(extension)
				|| "eot".equalsIgnoreCase(extension)
				|| "svg".equalsIgnoreCase(extension)
				|| "ttf".equalsIgnoreCase(extension)) {
			chain.doFilter(request, response);
			return;
		}
		if (checkUrl(url) || url.contains("/api/")) {
			chain.doFilter(request, response);
			return;
		}
		if (session.getAttribute(Constants.SESSION_USER_KEY) == null) {
			String requestType = request.getHeader("X-Requested-With");
			if(StringUtils.isNotBlank(requestType)){
				String forwardUrl = "/lampserver/login.html";
				response.sendRedirect(forwardUrl);
			}else{
				String forwardUrl = "/lampserver/login.html";
				response.sendRedirect(forwardUrl);
			}
			return;
		}
		chain.doFilter(request, response);
		return;
	}


//	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}

	public boolean checkUrl(String url) {
		String name = url.substring(url.lastIndexOf("/") + 1);
		for (String key : passUrl) {
			if (key.equals(name)) {
				return true;
			}
		}
		if(url.contains("/upload/")){
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		String aa = "http://localhots:8080/interphone/user/userphone";
		System.out.println(aa.substring(aa.lastIndexOf("/") + 1));
	}
}
